package com.yeskery.nut.extend.mcp.server;

import com.yeskery.nut.application.NutApplication;
import com.yeskery.nut.http.BaseResponse;
import com.yeskery.nut.util.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PrintStream;

/**
 * Mcp Stdio 响应对象
 * @author sprout
 * 2025-05-21 11:06
 */
public class StdioResponse extends BaseResponse {

    /** 输出流 */
    private final PrintStream printStream;

    /**
     * 构建Mcp Stdio 响应对象
     * @param nutApplication nut应用对象
     * @param printStream 输出流
     */
    protected StdioResponse(NutApplication nutApplication, PrintStream printStream) {
        super(nutApplication);
        this.printStream = printStream;
    }

    @Override
    protected void writeHeaderBytes(byte[] headers) {
    }

    @Override
    protected void writeBodyBytes(byte[] bytes) {
        writeBytes(new byte[0], bytes);
    }

    @Override
    protected void writeBodyInputStream(InputStream inputStream) {
        writeInputStream(new byte[0], inputStream);
    }

    @Override
    protected void writeBytes(byte[] headers, byte[] bytes) {
        writeInputStream(new byte[0], new ByteArrayInputStream(bytes));
    }

    @Override
    protected void writeInputStream(byte[] headers, InputStream inputStream) {
        IOUtils.transferInputStream(inputStream, printStream);
        output = true;
    }
}
